package com.example.notepad

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class GoalListActivity : AppCompatActivity() {

    private lateinit var goalListView: ListView
    private lateinit var goalEditText: EditText
    private lateinit var setGoalButton: Button
    private lateinit var clearGoalsButton: Button // 清空目标按钮
    private var goals = mutableListOf<String>()
    private var goalStates = mutableListOf<String>() // 用于存储每个目标的状态
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_goal_list)

        goalListView = findViewById(R.id.goalListView)
        goalEditText = findViewById(R.id.goalEditText)
        setGoalButton = findViewById(R.id.setGoalButton)
        clearGoalsButton = findViewById(R.id.clearGoalsButton) // 初始化清空目标按钮

        // 初始化 SharedPreferences 用于存储目标状态
        sharedPreferences = getSharedPreferences("GoalPrefs", MODE_PRIVATE)

        // 从 SharedPreferences 加载目标和状态
        loadGoals()

        // 设置目标按钮的点击事件
        setGoalButton.setOnClickListener {
            val newGoal = goalEditText.text.toString()
            if (newGoal.isNotEmpty()) {
                goals.add(newGoal)
                goalStates.add("未完成") // 默认新目标的状态是未完成
                saveGoals() // 保存目标
                goalEditText.text.clear() // 清空输入框
                updateGoalList() // 更新目标列表
            } else {
                Toast.makeText(this, "请输入目标内容", Toast.LENGTH_SHORT).show()
            }
        }

        // 清空目标按钮的点击事件
        clearGoalsButton.setOnClickListener {
            showClearDialog() // 显示清空目标的确认对话框
        }

        // 设置长按事件来更改目标状态
        goalListView.setOnItemLongClickListener { _, _, position, _ ->
            showStatusDialog(position)
            true
        }

        // 初始化目标列表
        updateGoalList()
    }

    // 显示状态选择对话框（已完成、部分完成、未完成）
    private fun showStatusDialog(position: Int) {
        val goal = goals[position]
        val statuses = arrayOf("已完成", "部分完成", "未完成")

        // 显示状态选择菜单
        AlertDialog.Builder(this)
            .setTitle("选择目标状态")
            .setItems(statuses) { _, which ->
                goalStates[position] = statuses[which] // 设置目标状态
                Toast.makeText(this, "目标 '$goal' 状态已更新为: ${statuses[which]}", Toast.LENGTH_SHORT).show()
                saveGoals() // 保存更新后的目标和状态
                updateGoalList() // 更新目标列表显示
            }
            .show()
    }

    // 显示清空目标的确认对话框
    private fun showClearDialog() {
        AlertDialog.Builder(this)
            .setTitle("清空所有目标")
            .setMessage("确定要清空所有目标吗？")
            .setPositiveButton("确定") { dialog, which ->
                goals.clear()
                goalStates.clear()
                saveGoals() // 保存目标
                updateGoalList() // 更新目标列表
                Toast.makeText(this, "所有目标已清空", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton("取消", null)
            .show()
    }

    // 更新 ListView 中显示的目标和状态
    private fun updateGoalList() {
        val updatedGoals = goals.mapIndexed { index, goal ->
            "$goal - 状态: ${goalStates[index]}"
        }

        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, updatedGoals)
        goalListView.adapter = adapter
    }

    // 保存目标和状态到 SharedPreferences
    private fun saveGoals() {
        val editor = sharedPreferences.edit()
        editor.putInt("goalCount", goals.size)
        goals.forEachIndexed { index, goal ->
            editor.putString("goal_$index", goal)
            editor.putString("status_$index", goalStates[index])
        }
        editor.apply()
    }

    // 从 SharedPreferences 加载目标和状态
    private fun loadGoals() {
        val goalCount = sharedPreferences.getInt("goalCount", 0)
        for (i in 0 until goalCount) {
            val goal = sharedPreferences.getString("goal_$i", null)
            val status = sharedPreferences.getString("status_$i", "未完成")
            if (goal != null) {
                goals.add(goal)
                goalStates.add(status ?: "未完成")
            }
        }
    }
}